/**
 * Copyright 2011 Snoopy Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.snoopycp.ui;

import com.googlecode.snoopyd.driver.IConfigurerPrx;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;

public class NodePropertiesInternalFrame extends javax.swing.JInternalFrame {

    IConfigurerPrx config;

    /** Creates new form NodeInfoInternalFrame */
    // FIXME change way of transfer IConfigurerPrx
    public NodePropertiesInternalFrame(Map<String, String> _info, String _nodeName, IConfigurerPrx _config) {
        initComponents();
        this.setClosable(true);
        this.setTitle(_nodeName + " - properties");
        initInfo(_info);
        config = _config;
    }

    public NodePropertiesInternalFrame(Map<String, String> _info) {
        initComponents();
        this.setClosable(true);
        this.setTitle("Properties");
        initInfo(_info);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCloseProperties = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        textAddressString = new javax.swing.JTextField();
        btnDataSourceConfirm = new javax.swing.JButton();
        jlExamplDataSourceStrting = new javax.swing.JLabel();
        btnViewCurrent = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        textPort = new javax.swing.JTextField();
        textBasename = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        textUsername = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        textPassword = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jlCPU = new javax.swing.JLabel();
        jlVendor = new javax.swing.JLabel();
        jlCores = new javax.swing.JLabel();
        jlFrequency = new javax.swing.JLabel();
        jlRAM = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jlHostname = new javax.swing.JLabel();
        jlIP = new javax.swing.JLabel();
        jlGateway = new javax.swing.JLabel();
        jlPrimDNS = new javax.swing.JLabel();

        setTitle("Properties");

        btnCloseProperties.setText("Close properties");
        btnCloseProperties.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClosePropertiesActionPerformed(evt);
            }
        });

        jLabel7.setText("Data source:");

        btnDataSourceConfirm.setText("Confirm");
        btnDataSourceConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDataSourceConfirmActionPerformed(evt);
            }
        });

        jlExamplDataSourceStrting.setText("Basename");

        btnViewCurrent.setText("View current");
        btnViewCurrent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewCurrentActionPerformed(evt);
            }
        });

        jLabel8.setText("Address");

        jLabel9.setText(":");

        jLabel10.setText("Username");

        jLabel11.setText("Password");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnViewCurrent)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 112, Short.MAX_VALUE)
                        .addComponent(btnDataSourceConfirm))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jlExamplDataSourceStrting)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addGap(35, 35, 35)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(textAddressString, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textPort, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(textBasename, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(textPassword, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(textUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(textAddressString, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlExamplDataSourceStrting)
                    .addComponent(textBasename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDataSourceConfirm)
                    .addComponent(btnViewCurrent))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Configure", jPanel1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Hardware info");

        jLabel2.setText("CPU");

        jLabel3.setText("Core");

        jLabel4.setText("Frequency");

        jLabel5.setText("RAM");

        jLabel6.setText("Vendor");

        jlCPU.setText("jLabel7");

        jlVendor.setText("jLabel8");

        jlCores.setText("jLabel9");

        jlFrequency.setText("jLabel10");

        jlRAM.setText("jLabel11");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setText("Network");

        jLabel13.setText("Internet address");

        jLabel14.setText("Default gateway");

        jLabel15.setText("Primary DNS");

        jLabel16.setText("Hostname");

        jlHostname.setText("jLabel17");

        jlIP.setText("jLabel18");

        jlGateway.setText("jLabel19");

        jlPrimDNS.setText("jLabel20");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel12)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel6)))
                                    .addComponent(jLabel5))
                                .addGap(59, 59, 59)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jlRAM)
                                    .addComponent(jlFrequency)
                                    .addComponent(jlCores)
                                    .addComponent(jlCPU)
                                    .addComponent(jlVendor)))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14)
                            .addComponent(jLabel15))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlPrimDNS)
                            .addComponent(jlGateway)
                            .addComponent(jlIP)
                            .addComponent(jlHostname))))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jlCPU)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jlVendor)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jlCores)
                                .addGap(20, 20, 20))
                            .addComponent(jlFrequency))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlRAM)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel15))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jlHostname)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlIP)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlGateway)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlPrimDNS)))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("NodeInfo", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(199, Short.MAX_VALUE)
                .addComponent(btnCloseProperties)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(11, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCloseProperties)
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("Configure");
        jTabbedPane1.getAccessibleContext().setAccessibleDescription("Configure");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnClosePropertiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClosePropertiesActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnClosePropertiesActionPerformed

    private void btnDataSourceConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDataSourceConfirmActionPerformed
        if (!this.textAddressString.getText().equalsIgnoreCase("") && !this.textPort.getText().equalsIgnoreCase("")
                && !this.textBasename.getText().equalsIgnoreCase("") && !this.textUsername.getText().equalsIgnoreCase("")) {
            Map<String, String> map = new HashMap<String, String>();
            //String sourceString = this.textDataSourceString.getText();
                String address = this.textAddressString.getText();
                String port = this.textPort.getText();
                String basename = this.textBasename.getText();
                String username = this.textUsername.getText();
                String password = new String(this.textPassword.getPassword());
                String url = "jdbc:mysql://" + address + ":" + port + "/" +
                        basename + "?user=" + username + "&password=" + password;
                map.put("connectionstring", url);
                try {
                    config.ice_ping();
                    config.reconfigure(map);
                } catch (Ice.ConnectionRefusedException e) {
                    JOptionPane.showInternalMessageDialog(this, "Node not avalibele more " + e.getMessage());
                }
        } else {
            JOptionPane.showInternalMessageDialog(this, "Some field is empty");
        }

    }//GEN-LAST:event_btnDataSourceConfirmActionPerformed

    private void btnViewCurrentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewCurrentActionPerformed
        String string = null;
        try {
            string = config.configuration().get("connectionstring");
            String[] strings = string.split("/");
            String address = strings[2];
            String[] tmp = strings[3].split("=");
            String basename = tmp[0].substring(0, tmp[0].length() - 5);
            String user = tmp[1].split("&")[0];
            String pass = tmp[2];
            String str = "\naddress: " + address + "\nbasename: " + basename
                    + "\nusername: " + user + "\npassword: " + pass;
            config.ice_ping();
            JOptionPane.showInternalMessageDialog(this, str, "Current data source string", JOptionPane.INFORMATION_MESSAGE);
        } catch (Ice.ConnectionRefusedException e) {
            JOptionPane.showInternalMessageDialog(this, "Node not avalibele more " + e.getMessage());
        } catch (ArrayIndexOutOfBoundsException e) {
            JOptionPane.showInternalMessageDialog(this, string, "Current data source string", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnViewCurrentActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCloseProperties;
    private javax.swing.JButton btnDataSourceConfirm;
    private javax.swing.JButton btnViewCurrent;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel jlCPU;
    private javax.swing.JLabel jlCores;
    private javax.swing.JLabel jlExamplDataSourceStrting;
    private javax.swing.JLabel jlFrequency;
    private javax.swing.JLabel jlGateway;
    private javax.swing.JLabel jlHostname;
    private javax.swing.JLabel jlIP;
    private javax.swing.JLabel jlPrimDNS;
    private javax.swing.JLabel jlRAM;
    private javax.swing.JLabel jlVendor;
    private javax.swing.JTextField textAddressString;
    private javax.swing.JTextField textBasename;
    private javax.swing.JPasswordField textPassword;
    private javax.swing.JTextField textPort;
    private javax.swing.JTextField textUsername;
    // End of variables declaration//GEN-END:variables

    private void initInfo(Map<String, String> _info) {
        this.jlCPU.setText(_info.get("Model"));// FIXME may be very long name
        this.jlCores.setText(_info.get("TotalCores"));
        this.jlVendor.setText(_info.get("Vendor"));
        this.jlFrequency.setText(_info.get("Mhz") + " MHz");
        this.jlRAM.setText(_info.get("Free").substring(0, 3) + " / " + _info.get("Ram") + " MB");
        this.jlHostname.setText(_info.get("HostName"));
        this.jlIP.setText(_info.get("IP"));
        this.jlGateway.setText(_info.get("DefaultGateway"));
        this.jlPrimDNS.setText(_info.get("PrimaryDns"));
    }
}
